package com.zoo.diannaopeijian.controller.component;

import com.zoo.diannaopeijian.common.page.Page;
import com.zoo.diannaopeijian.common.tree.TreeUtil;
import com.zoo.diannaopeijian.controller.BaseController;
import com.zoo.diannaopeijian.domain.Ware;
import com.zoo.diannaopeijian.domain.WareParameterDetail;
import com.zoo.diannaopeijian.service.WareParameterDetailService;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * 商品配件参数属性控制器
 */
@Controller
@RequestMapping("/wareParameterDetail")
public class WareParameterDetailController extends BaseController {
    @Resource
    private WareParameterDetailService wareParameterDetailService;

    @PostMapping("/")
    public ResponseEntity<String> wareParameterDetailPost(WareParameterDetail wareParameterDetail) {
        return ResponseEntity.ok(wareParameterDetailService.addWareParameterDetail(wareParameterDetail) ? success : fail);
    }

    @DeleteMapping("/")
    public ResponseEntity<String> wareParameterDetailDelete(WareParameterDetail wareParameterDetail) {
        return ResponseEntity.ok(wareParameterDetailService.removeWareParameterDetail(wareParameterDetail) ? success : fail);
    }

    @GetMapping("/")
    public ResponseEntity<List<Map>> wareParameterDetailGet(Integer wareId) {
        List<String> nodeName = new ArrayList<>();
        nodeName.add("component");
        nodeName.add("parameter");
        nodeName.add("detail");

        return ResponseEntity.ok(TreeUtil.buildEasyUITree(wareParameterDetailService.getWareParameterDetail(wareId), nodeName));
    }

    @GetMapping("/parameterDetailWare/list")
    public ResponseEntity<Map<String, Object>> parameterDetailWareListGet(
            Page page,
            @RequestParam(required = true, defaultValue = "false") boolean isFuzzy,
            Integer componentId,
            String componentParameterDetailIds,
            Ware ware
    ) {
        List<Integer> comParamDetails = new ArrayList<>();
        if (!StringUtils.isEmpty(componentParameterDetailIds)) {
            for (String detail : componentParameterDetailIds.split(" ")) {
                comParamDetails.add(Integer.parseInt(detail));
            }
        }
        return ResponseEntity.ok(buildPageResult(isFuzzy ? wareParameterDetailService.getFuzzyParameterDetailWareWithPage(ware, page, componentId, comParamDetails) : wareParameterDetailService.getParameterDetailWareWithPage(ware, page, componentId, comParamDetails)));
    }
}
